Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.605 lượt xem;
- NetworkCharacter.cs
- MarcoPolo-Tutorial /
- Demos /
- Photon Unity Networking /
- Assets /
- project /
1 using UnityEngine;
2
3 public class NetworkCharacter : Photon.MonoBehaviour
4 {
5 private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this
6 private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this
7 // Update is called once per frame
8 void Update()
9 {
10 if (!photonView.isMine)
11 {
12 transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
13 transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
14 }
15 }
16
17 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
18 {
19 if (stream.isWriting)
20 {
21 // We own this player: send the others our data
22 stream.SendNext(transform.position);
23 stream.SendNext(transform.rotation);
24
25 myThirdPersonController myC = GetComponent<myThirdPersonController>();
26 stream.SendNext((int)myC._characterState);
27 }
28 else
29 {
30 // Network player, receive data
31 this.correctPlayerPos = (Vector3)stream.ReceiveNext();
32 this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
33
34 myThirdPersonController myC = GetComponent<myThirdPersonController>();
35 myC._characterState = (CharacterState)stream.ReceiveNext();
36 }
37 }
38 }
2
3 public class NetworkCharacter : Photon.MonoBehaviour
4 {
5 private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this
6 private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this
7 // Update is called once per frame
8 void Update()
9 {
10 if (!photonView.isMine)
11 {
12 transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
13 transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
14 }
15 }
16
17 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
18 {
19 if (stream.isWriting)
20 {
21 // We own this player: send the others our data
22 stream.SendNext(transform.position);
23 stream.SendNext(transform.rotation);
24
25 myThirdPersonController myC = GetComponent<myThirdPersonController>();
26 stream.SendNext((int)myC._characterState);
27 }
28 else
29 {
30 // Network player, receive data
31 this.correctPlayerPos = (Vector3)stream.ReceiveNext();
32 this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
33
34 myThirdPersonController myC = GetComponent<myThirdPersonController>();
35 myC._characterState = (CharacterState)stream.ReceiveNext();
36 }
37 }
38 }
private Vector3 correctPlayerPos = Vector3.zero; We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; We lerp towards this
Update is called once per frame
We own this player: send the others our data
Network player, receive data